//https://leetcode.cn/problems/minimum-number-of-frogs-croaking/

class Solution {
public:
    int minNumberOfFrogs(string croakOfFrogs) {
        string s = "croak";
        int len = s.size();
        unordered_map<char, int> index;
        for (int i = 0; i < len; i++)
        {
            index[s[i]] = i;
        }

        vector<int> hash(len);

        for (auto e : croakOfFrogs)
        {
            if (e == 'c')
            {
                if (hash[len - 1] != 0)
                    hash[len - 1]--;
                hash[index[e]]++;
            }
            else
            {
                int i = index[e];
                if (hash[i - 1] == 0) return -1;
                hash[i]++;
                hash[i - 1]--;
            }
        }

        for (int i = 0; i < len - 1; i++)
        {
            if (hash[i] != 0)
                return -1;
        }

        return hash[len - 1];

    }
};